import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:pmviewer/app/routes/app_pages.dart';

import '../controllers/login_controller.dart';

class LoginView extends GetView<LoginController> {
  final _formKey = GlobalKey<FormState>();
  LoginView({super.key});
  @override
  Widget build(BuildContext context) {
    TextEditingController username = TextEditingController();
    TextEditingController password = TextEditingController();
    username.text = '00212';
    password.text = '123';
    void doLogin() async {
      if (!_formKey.currentState!.validate()) {
        return;
      }

      // 转去主页
      Get.offAllNamed(Routes.HOME);
    }

    var userNameField = TextFormField(
      controller: username,
      validator: (value) => value!.isEmpty ? 'input_username'.tr : null,
      style: TextStyle(
        fontSize: 20.sp,
        fontWeight: FontWeight.normal,
      ),
      decoration: InputDecoration(labelText: 'input_username'.tr),
    );

    var userPassField = TextFormField(
      controller: password,
      obscureText: true,
      validator: (value) => value!.isEmpty ? 'input_password'.tr : null,
      style: TextStyle(
        fontSize: 20.sp,
        fontWeight: FontWeight.normal,
      ),
      decoration: InputDecoration(labelText: 'input_password'.tr),
    );

    var loginButton = ElevatedButton(
      onPressed: doLogin,
      child: SizedBox(
        width: 350.w,
        height: 50.h,
        child: Center(
          child: Text(
            'login'.tr,
            style: TextStyle(fontSize: 20.sp, fontWeight: FontWeight.normal),
          ),
        ),
      ),
    );

    var serverAddressAndSettings = Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('',
            style: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.normal)),
        SizedBox(width: 20.w),
        IconButton(
            onPressed: () => Get.toNamed(Routes.SERVER_SETTINGS),
            icon: const Icon(Icons.settings)),
      ],
    );
    return Scaffold(
      body: SingleChildScrollView(
          child: Center(
              child: Padding(
        padding: EdgeInsets.all(20.w),
        child: Column(
          children: [
            SizedBox(height: 10.w),
            Opacity(
                opacity: 0.36,
                child: Image.asset(
                  'assets/images/logo_ecg.png',
                )),
            SizedBox(height: 10.h),
            Text('Login_Info'.tr,
                style: Theme.of(context).textTheme.headlineMedium),
            SizedBox(height: 50.h),
            Form(
              key: _formKey,
              child: Column(
                children: [
                  userNameField,
                  SizedBox(height: 50.h),
                  userPassField,
                  SizedBox(height: 30.h),
                ],
              ),
            ),
            SizedBox(height: 40.h),
            loginButton,
            SizedBox(height: 20.h),
            serverAddressAndSettings,
            // const SizedBox(height: 20),
          ],
        ),
      ))),
    );
  }
}
